/**
 * @Author :sunwei
 * @Description:
 * @Date create in 22:36  2021/5/23
 * @Modified by:
 */
public class MultiThreadVersion {

    private final String LOCK = "LOCK";

    private int index =0;

    private volatile boolean isProduce = false;

    public void produce(){
        synchronized (LOCK){

            while(isProduce){
                try {
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " P -->" + ++index);
            isProduce = !isProduce;
            //唤醒所有消费者和生产者
            LOCK.notifyAll();
        }
    }
    public void consume(){
        synchronized (LOCK) {
            while (!isProduce) {
                try {
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " C -->" + index);
            isProduce = !isProduce;
            //唤醒所有消费者和生产者
            LOCK.notifyAll();
        }
    }
}
